package com.bxd.baodan.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.google.gson.Gson;
import com.bxd.baodan.App;
import com.bxd.baodan.R;
import com.bxd.baodan.app.Constant;
import com.bxd.baodan.app.JsonApi;
import com.bxd.baodan.item.DesignCrInfoJSONBean;
import com.bxd.baodan.item.FamDesignBean;
import com.bxd.baodan.item.FamReportBean;
import com.bxd.baodan.item.GroupBean;
import com.bxd.baodan.item.LayoutBean;
import com.bxd.baodan.item.MemberInfo;
import com.bxd.baodan.util.GsonUtil;
import com.bxd.baodan.util.LogUtil;
import com.bxd.baodan.util.NetUtil;
import com.bxd.baodan.util.StringUtil;
import com.bxd.baodan.widget.IosSheetDialog;
import com.bxd.baodan.widget.IosSheetDialog.DialogItemListener;
import com.bxd.baodan.widget.MyTextWatcher;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AddMemeberActivity extends BaseActivity {

    List<FamDesignBean.ValueBean> jobList = new ArrayList<>();

    String[] jobStr;

    ListView lv;

    MAdapter adapter;

    List<GroupBean> list = new ArrayList<>();


    List<LayoutBean> layouts = new ArrayList<>();

    int[] myIds = {R.layout.family_item_gender, R.layout.family_item_age, R.layout.family_item_job, R.layout.family_item_income,
            R.layout.family_item_shebao, R.layout.family_item_other_baoxian};
    int[] spouseIds = {R.layout.family_item_age, R.layout.family_item_job, R.layout.family_item_income,
            R.layout.family_item_shebao, R.layout.family_item_other_baoxian};

    String[] myTexts = {"的性别", "的年龄", "的职业", "的个人年收入", "的社保情况", "的其它保险情况"};
    String[] spouseTexts = {"的年龄", "的职业", "的个人年收入", "的社保情况", "的其它保险情况"};

    int[] motherIds = {R.layout.family_item_age, R.layout.family_item_shebao, R.layout.family_item_other_baoxian};
    int[] fatherIds = {R.layout.family_item_age, R.layout.family_item_shebao, R.layout.family_item_other_baoxian};
    String[] parentTexts = {"的年龄", "的社保情况", "的其它保险情况"};

    int[] childIds = {R.layout.family_item_gender, R.layout.family_item_age, R.layout.family_item_shebao, R.layout.family_item_other_baoxian,
            R.layout.family_item_study};
    String[] childTexts = {"的性别", "的年龄", "的社保情况", "的其它保险情况", "的教育计划"};

    String currentAge, currentJob, yearIncome, haveShebao, currentEducationPlan, haveOther, currentGender = "";


    String[] otherStrs = new String[6];
    String[] homeSpendStrs = new String[4];
    String[] homeSpend = new String[4];

    DesignCrInfoJSONBean bean;

    private int checkPosition = -1;

    MemberInfo info;

    SeekBar seekBar;

    int maxStep;
    int currentStep;


    int child_num;

    private String memberCall;

    Map<Integer, Boolean> map = new HashMap<>();

    int addSize = -1;
    int animPosition = -1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_family_design);
        setActionBarTitle("添加家庭成员");

        initView();
        getDesignData();

        String data = getIntent().getStringExtra("fam_info");
        bean = (DesignCrInfoJSONBean) GsonUtil.fromJson(data, DesignCrInfoJSONBean.class);


        GroupBean first = new GroupBean();
        first.middleVisible = true;
        first.layout = R.layout.family_add_start;
        list.add(first);


        setAdapterListener();


        lv.setAdapter(adapter);


    }

    private void setAdapterListener() {
        adapter = new MAdapter(this, list);
        adapter.setListener(new OkListener() {
            @Override
            public void next(int position, GroupBean nextBean) {

                GroupBean current = list.get(position);
                current.leftVisible = true;
                current.rightVisible = true;
                current.middleVisible = false;
                current.waitVisible = false;

                nextBean.leftVisible = true;
                nextBean.rightVisible = true;
                nextBean.middleVisible = true;
                current.waitVisible = true;


                if (current.layout == R.layout.family_add_start) {
                    list.remove(current);
                }


                list.add(nextBean);

                addSize = list.size();
                adapter.notifyDataSetChanged();

//                lv.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
//                    @Override
//                    public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
//                     //   lv.removeOnLayoutChangeListener(this);
//                        lv.smoothScrollToPosition(adapter.getCount() - 1);
//                    }
//                });

                lv.smoothScrollToPosition(adapter.getCount() - 1);

            }


            @Override
            public void pre(int position) {

                if (list.get(position - 1).layout == R.layout.family_add_member) {
                    layouts.clear();
                }


                list.get(position - 1).middleVisible = true;
                list.remove(position);


                // currentSize = list.size();
                adapter.notifyDataSetChanged();
                lv.setSelection(adapter.getCount() - 1);
                currentStep--;
                if (currentStep < 0) {
                    seekBar.setVisibility(View.GONE);
                } else {
                    seekBar.setVisibility(View.VISIBLE);
                    seekBar.setProgress(currentStep * 100 / maxStep);
                }

            }
        });
    }

    private void initView() {
        lv = (ListView) findViewById(R.id.lv);
        seekBar = findViewById(R.id.seekBar);
        seekBar.setVisibility(View.GONE);
        seekBar.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });


        findViewById(R.id.bar_iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                showExitDialog("确认退出添加成员吗?");

            }
        });
    }

    private void getDesignData() {
        NetUtil netUtil = new NetUtil(this, JsonApi.DESIGN_CONDITION);
        netUtil.setParams("type", "1");
        netUtil.postRequest("加载中", new NetUtil.RequestStringListener() {
            @Override
            public void onComplete(String result) {
                FamDesignBean bean = new Gson().fromJson(result, FamDesignBean.class);
                List<FamDesignBean.ValueBean> careerValue = bean.data.condition.career.value;
                jobList.clear();
                jobList.addAll(careerValue);

                jobStr = new String[jobList.size()];

                for (int i = 0; i < jobList.size(); i++) {
                    jobStr[i] = jobList.get(i).name;
                }


            }
        });
    }


    class MAdapter extends BaseAdapter {
        private List<GroupBean> list;
        private Context context;
        private OkListener listener;


        MAdapter(Context context, List<GroupBean> list) {
            this.list = list;
            this.context = context;
        }


        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {

            final ViewHolder holder;
            final GroupBean item = list.get(i);
            if (view == null) {
                view = View.inflate(AddMemeberActivity.this, R.layout.design_list_item, null);
                holder = new ViewHolder();
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }


            holder.left = (TextView) view.findViewById(R.id.left);
            holder.right = (TextView) view.findViewById(R.id.right);
            holder.middle = (LinearLayout) view.findViewById(R.id.middle);
            holder.wait_layout = (RelativeLayout) view.findViewById(R.id.wait_layout);


            holder.wait_layout.setVisibility(View.GONE);
            holder.left.setVisibility(item.leftVisible ? View.VISIBLE : View.GONE);
            holder.right.setVisibility(item.rightVisible ? View.VISIBLE : View.GONE);
            holder.middle.setVisibility(item.middleVisible ? View.VISIBLE : View.GONE);


            holder.left.setText(item.left);
            holder.right.setText(item.right);
            final ViewGroup mid = (ViewGroup) View.inflate(context, item.layout, null);
            holder.middle.removeAllViews();

// FIXME: 18/1/18 回退的时候也不能动画


            if (i == list.size() - 1 && i == addSize - 1 && addSize - 1 != animPosition) {

                setWaiting(i, holder, mid);

            } else {
                holder.middle.removeAllViews();
                holder.middle.addView(mid);
            }


            if (item.middleVisible) {


                try {
                    if (layouts != null && layouts.size() > 0) {
                        TextView title = ((TextView) mid.findViewById(R.id.title));
                        title.setText(layouts.get(i - 1).text);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }


                switch (item.layout) {


                    case R.layout.family_add_member:
                        parseMember(mid);

                        break;

                    case R.layout.family_item_gender:
                        parseGender(item, mid);

                        break;

                    case R.layout.family_item_age:
                        parseAge(item, mid);

                        break;
                    case R.layout.family_item_job:
                        parseJob(item, mid);
                        break;
                    case R.layout.family_item_income:
                        parseIncome(item, mid, i);
                        break;

                    case R.layout.family_item_shebao:
                        parseSheBao(item, mid);
                        break;

                    case R.layout.family_item_other_baoxian:
                        parseOther(item, mid);
                        break;

                    case R.layout.family_item_study:
                        parseStudy(item, mid);
                        break;

                    case R.layout.family_item_home_income:
                        parseHomeIncome(item, mid);
                        break;
                }


                setMidListener(i, item, mid);
            }

            return view;
        }

        private void setMidListener(final int i, final GroupBean item, ViewGroup mid) {
            try {
                mid.findViewById(R.id.finish).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        bean.memberInfos.add(info);
                        String json = new Gson().toJson(bean.memberInfos);
                        bean.memberInfos.remove(info);
                        LogUtil.e("json: " + json);


                        NetUtil netUtil = new NetUtil(AddMemeberActivity.this, JsonApi.DESIGN_FAMILY);

                        netUtil.setParams("familyInfo", json);
                        netUtil.setParams("action", "doReport");


                        netUtil.setParams("familyAnnualIncome", homeSpend[0]);
                        netUtil.setParams("familyMonthSpending", homeSpend[1]);
                        netUtil.setParams("familyMonthAlimony", homeSpend[2]);
                        netUtil.setParams("familyLoan", homeSpend[3]);

                        netUtil.postRequest("正在生成报告", new NetUtil.RequestStringListener() {
                            @Override
                            public void onComplete(String result) {
                                FamReportBean bean = new Gson().fromJson(result, FamReportBean.class);
                                MobclickAgent.onEvent(App.getContext(), "v6_0_0_SY_znfasj_jt_ckbg");
                                openActivity(WebViewPager.class, new String[]{"startUrl"}, new String[]{bean.data.url});
                                App.getContext().sendBroadcast(
                                        new Intent(Constant.ACTION_SUCCESS_FAMILY_REPORT_SUCESS));

                                finish();
                            }
                        });

                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }


            mid.findViewById(R.id.next).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {


                    GroupBean nextBean = new GroupBean();
                    if (layouts != null && layouts.size() > 0 && i < layouts.size()) {
                        nextBean.member = layouts.get(i).member;
                        nextBean.name = layouts.get(i).text;
                        nextBean.left = layouts.get(i - 1).text;
                        nextBean.layout = layouts.get(i).layout;
                    }

                    switch (item.layout) {
                        case R.layout.family_add_start:
                            nextBean.left = "请输入家庭成员的基本信息,并重新确认家庭收支状况,以便更新您的家庭保障状态~";
                            nextBean.right = "开始吧";
                            nextBean.layout = R.layout.family_add_member;

                            break;

                        case R.layout.family_add_member:
                            if (checkPosition == -1) {
                                showToast("请添加一名家庭成员");
                                return;
                            }

                            getLayouts(checkPosition);

                            nextBean.member = layouts.get(i).member;
                            nextBean.name = layouts.get(i).text;
                            nextBean.layout = layouts.get(i).layout;
                            nextBean.left = "您添加的家庭成员";
                            nextBean.right = memberCall;
                            break;


                        case R.layout.family_item_gender:
                            if (StringUtil.isEmpty(currentGender)) {
                                showToast("请完善信息");
                                return;
                            }
                            nextBean.right = currentGender;
                            break;

                        case R.layout.family_item_age:
                            if (StringUtil.isEmpty(currentAge)) {
                                showToast("请完善信息");
                                return;
                            }
                            nextBean.right = currentAge + "岁";
                            break;
                        case R.layout.family_item_job:
                            if (StringUtil.isEmpty(currentJob)) {
                                showToast("请完善信息");
                                return;
                            }
                            nextBean.right = currentJob;
                            break;
                        case R.layout.family_item_income:
                            if (StringUtil.isEmpty(yearIncome)) {
                                showToast("请完善信息");
                                return;
                            }
                            nextBean.right = yearIncome + "万元";
                            break;

                        case R.layout.family_item_shebao:
                            if (StringUtil.isEmpty(haveShebao)) {
                                showToast("请完善信息");
                                return;
                            }
                            nextBean.right = haveShebao;
                            break;


                        case R.layout.family_item_other_baoxian:
                            String other;
                            StringBuilder b = new StringBuilder();
                            if (haveOther.equals("no")) {
                                other = "无";
                            } else if (haveOther.equals("have")) {
                                b.append("有\n");
                                boolean isEmpty = true;

                                for (String otherStr : otherStrs) {
                                    if (!StringUtil.isEmpty(otherStr)) {
                                        isEmpty = false;
                                        break;
                                    }
                                }
                                if (isEmpty) {
                                    showToast("请完善信息");
                                    return;
                                }


                                for (int j = 0; j < otherStrs.length; j++) {
                                    if (!StringUtil.isEmpty(otherStrs[j])) {
                                        switch (j) {
                                            case 0:
                                                b.append("人寿保险保额 :   " + otherStrs[j] + "万元\n");

                                                break;

                                            case 1:
                                                b.append("重疾保险保额 :   " + otherStrs[j] + "万元\n");
                                                break;

                                            case 2:
                                                b.append("意外保险保额 :   " + otherStrs[j] + "万元\n");
                                                break;


                                            case 3:
                                                b.append("医疗保险保额 :   " + otherStrs[j] + "万元\n");
                                                break;


                                            case 4:
                                                b.append("养老保险保额 :   " + otherStrs[j] + "万元\n");
                                                break;


                                            case 5:
                                                b.append("教育金 :   " + otherStrs[j] + "万元\n");
                                                break;
                                        }


                                    }


                                }
                                String str = b.toString();
                                if (str.endsWith("\n")) {
                                    int index = str.lastIndexOf("\n");
                                    str = str.substring(0, index);
                                }
                                other = str;

                            } else {
                                showToast("请先完善信息");
                                return;
                            }
                            nextBean.right = other;
                            break;
                        case R.layout.family_item_study:

                            nextBean.right = currentEducationPlan;
                            break;
                        case R.layout.family_item_home_income:
                            StringBuilder stringBuilder = new StringBuilder();

                            for (String homeSpendStr : homeSpendStrs) {
                                if (StringUtil.isEmpty(homeSpendStr)) {
                                    showToast("请填写所有信息");
                                    return;
                                }
                                stringBuilder.append(homeSpendStr);
                            }

                            nextBean.right = stringBuilder.toString();
                            break;
                    }

                    if (maxStep > 0) {
                        currentStep++;
                        seekBar.setVisibility(View.VISIBLE);
                        seekBar.setProgress(currentStep * 100 / maxStep);
                    }
                    listener.next(i, nextBean);


                }
            });

            try {
                mid.findViewById(R.id.bar_iv_close).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {


                        listener.pre(i);
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        private void setWaiting(int i, final ViewHolder holder, final ViewGroup mid) {
            animPosition = i;
            ViewGroup waitLy = (ViewGroup) View.inflate(context, R.layout.design_waiting_layout, null);
            holder.wait_layout.addView(waitLy);


            ImageView waitIv = (ImageView) waitLy.findViewById(R.id.wait);


            waitIv.setImageResource(R.drawable.in_animation_fam);
            final AnimationDrawable animationDrawable = (AnimationDrawable) waitIv.getDrawable();
            animationDrawable.start();
            holder.wait_layout.setVisibility(View.VISIBLE);

            waitIv.postDelayed(new Runnable() {
                @Override
                public void run() {

                    animationDrawable.stop();
                    holder.wait_layout.removeAllViews();
                    holder.wait_layout.setVisibility(View.GONE);
                    holder.middle.removeAllViews();
                    holder.middle.addView(mid);
                }
            }, 1000);
        }

        public void setListener(OkListener listener) {
            this.listener = listener;
        }

        class ViewHolder {
            TextView left;
            TextView right;
            LinearLayout middle;

            RelativeLayout wait_layout;
        }


    }

    private void parseGender(final GroupBean item, ViewGroup mid) {
        currentGender = "";

        RadioGroup genderRg = (RadioGroup) mid.findViewById(R.id.gender_rg);
        genderRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                currentGender = ((RadioButton) group.findViewById(checkedId)).getText().toString();


                if (info.role.equals(item.member)) {
                    //todo 添加社保id
                    if (checkedId == R.id.man) {
                        info.gender = "male";
                    } else if (checkedId == R.id.woman) {
                        info.gender = "female";
                    }
                }

                if (info.role.equals("spouse") && item.member.equals("my")) {
                    if (checkedId == R.id.man) {
                        info.gender = "female";
                    } else if (checkedId == R.id.woman) {
                        info.gender = "male";
                    }
                }
                if (info.role.equals("father")) {
                    info.gender = "male";
                }

                if (info.role.equals("mother")) {
                    info.gender = "female";

                }

            }
        });
    }


    private void parseHomeIncome(final GroupBean item, ViewGroup mid) {

        for (int i = 0; i < homeSpendStrs.length; i++) {
            homeSpendStrs[i] = "";
            homeSpend[i] = "";
        }

        //年收入
        final EditText year = (EditText) mid.findViewById(R.id.family_annual_income);
        //月收入
        final EditText mouth = (EditText) mid.findViewById(R.id.family_month_spending);
        //赡养金
        final EditText alimony = (EditText) mid.findViewById(R.id.family_month_alimony);
        //贷款
        final EditText loan = (EditText) mid.findViewById(R.id.family_loan);


        year.addTextChangedListener(new MyTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {

                // info.family_annual_income = year.getText().toString().trim();
                homeSpend[0] = year.getText().toString().trim();
                homeSpendStrs[0] = "家庭年收入:  " + year.getText().toString().trim() + "万元\n";

            }
        });


        mouth.addTextChangedListener(new MyTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                //  info.family_month_spending = mouth.getText().toString().trim();
                homeSpend[1] = mouth.getText().toString().trim();
                homeSpendStrs[1] = "家庭月开销:  " + mouth.getText().toString().trim() + "元/月\n";
            }
        });


        alimony.addTextChangedListener(new MyTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                // info.family_month_alimony = alimony.getText().toString().trim();
                homeSpend[2] = alimony.getText().toString().trim();
                homeSpendStrs[2] = "赡养金:  " + alimony.getText().toString().trim() + "元/月\n";
            }
        });


        loan.addTextChangedListener(new MyTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {

                // fixme 修复没有贷款,写错变量了
                //   info.family_loan = loan.getText().toString().trim();
                homeSpend[3] = loan.getText().toString().trim();
                homeSpendStrs[3] = "待偿还贷款:  " + loan.getText().toString().trim() + "万元";
            }
        });


    }


    private void parseStudy(final GroupBean item, ViewGroup mid) {
        currentEducationPlan = "";
        ((RadioGroup) mid.findViewById(R.id.rg_study)).setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                if (info.role.equals(item.member)) {

                    if (checkedId == R.id.domestic) {
                        info.educationPlan = "domestic";
                        currentEducationPlan = "国内大学";

                    } else if (checkedId == R.id.foreign) {
                        info.educationPlan = "foreign";
                        currentEducationPlan = "国外大学";

                    } else if (checkedId == R.id.no) {
                        info.educationPlan = "no";
                        currentEducationPlan = "暂无计划";
                    }
                }

            }
        });

    }

    private void parseOther(final GroupBean item, ViewGroup mid) {
        haveOther = "";
        for (int i = 0; i < otherStrs.length; i++) {
            otherStrs[i] = "";
        }
        final RadioGroup rg_other = (RadioGroup) mid.findViewById(R.id.rg_other);
        final View business_insure_ly = mid.findViewById(R.id.business_insure_ly);

        //人寿
        final EditText life = (EditText) mid.findViewById(R.id.business_insurance_life);
        //重疾
        final EditText serious = (EditText) mid.findViewById(R.id.business_insurance_serious);
        //意外
        final EditText accident = (EditText) mid.findViewById(R.id.business_insurance_accident);
        //医疗
        final EditText medical = (EditText) mid.findViewById(R.id.business_insurance_medical);
        //养老
        final EditText pension = (EditText) mid.findViewById(R.id.business_insurance_pension);
        //教育
        final EditText education = (EditText) mid.findViewById(R.id.business_insurance_education);


        life.addTextChangedListener(new MyTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {

                if (info.role.equals(item.member)) {
                    info.businessInsuranceLife = life.getText().toString().trim();
                    otherStrs[0] = life.getText().toString().trim();
                }

            }
        });


        serious.addTextChangedListener(new MyTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {

                if (info.role.equals(item.member)) {
                    info.businessInsuranceSerious = serious.getText().toString().trim();
                    otherStrs[1] = serious.getText().toString().trim();
                }

            }
        });


        accident.addTextChangedListener(new MyTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {

                if (info.role.equals(item.member)) {
                    info.businessInsuranceAccident = accident.getText().toString().trim();
                    otherStrs[2] = accident.getText().toString().trim();
                }

            }
        });


        medical.addTextChangedListener(new MyTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {

                if (info.role.equals(item.member)) {
                    info.businessInsuranceMedical = medical.getText().toString().trim();
                    otherStrs[3] = medical.getText().toString().trim();
                }

            }
        });


        pension.addTextChangedListener(new MyTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {

                if (info.role.equals(item.member)) {
                    info.businessInsurancePension = pension.getText().toString().trim();
                    otherStrs[4] = pension.getText().toString().trim();

                }
            }
        });


        education.addTextChangedListener(new MyTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {

                if (info.role.equals(item.member)) {
                    info.businessInsuranceEducation = education.getText().toString().trim();
                    otherStrs[5] = education.getText().toString().trim();
                }

            }
        });


        rg_other.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                haveOther = ((RadioButton) rg_other.findViewById(checkedId)).getText().toString();

                business_insure_ly.setVisibility(
                        checkedId == R.id.have ? View.VISIBLE : View.GONE);
                if (checkedId == R.id.have) {
                    haveOther = "have";
                } else if (checkedId == R.id.no) {
                    haveOther = "no";
                }


                if (info.role.equals(item.member)) {
                    //todo 添加社保id
                    if (checkedId == R.id.have) {
                        info.haveBusinessInsurance = "have";
                    } else if (checkedId == R.id.no) {
                        info.haveBusinessInsurance = "no";
                        info.businessInsuranceLife = null;
                        info.businessInsuranceSerious = null;
                        info.businessInsuranceAccident = null;
                        info.businessInsuranceMedical = null;
                        info.businessInsurancePension = null;
                        info.businessInsuranceEducation = null;

                    }

                }

            }
        });


    }

    private void parseSheBao(final GroupBean item, ViewGroup mid) {
        haveShebao = "";

        RadioGroup rg_shebao = (RadioGroup) mid.findViewById(R.id.rg_shebao);
        rg_shebao.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                haveShebao = ((RadioButton) group.findViewById(checkedId)).getText().toString();


                if (info.role.equals(item.member)) {
                    //todo 添加社保id
                    if (checkedId == R.id.have) {
                        info.haveSocialInsurance = "have";
                    } else if (checkedId == R.id.no) {
                        info.haveSocialInsurance = "no";
                    }

                }

            }
        });

    }


    private void parseIncome(final GroupBean item, ViewGroup mid, int i) {
        yearIncome = "";


        //修改个人收入中的文本内容
        if (layouts != null && layouts.size() > 0) {
            TextView income_tv = ((TextView) mid.findViewById(R.id.income_tv));
            income_tv.setText(layouts.get(i - 1).text);
        }

        final EditText et = ((EditText) mid.findViewById(R.id.et_income));
        et.addTextChangedListener(new MyTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {

                if (info.role.equals(item.member)) {
                    info.personalAnnualIncome = et.getText().toString();
                    yearIncome = et.getText().toString();
                }

            }
        });

    }


    private void parseJob(final GroupBean item, ViewGroup mid) {
        currentJob = "";

        final TextView job = ((TextView) mid.findViewById(R.id.job_tv));
        mid.findViewById(R.id.job_ly).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //弹窗

                IosSheetDialog dialog = new IosSheetDialog(AddMemeberActivity.this).
                        builder().
                        setCancelable(false).
                        setCanceledOnTouchOutside(true);
                dialog.setSheetItems(jobStr, new DialogItemListener() {
                    @Override
                    public void onClick(int position) {
                        job.setText(jobStr[position]);
                        currentJob = job.getText().toString().trim();
                        //选择的时候遍历

                        if (info.role.equals(item.member)) {
                            info.career = jobList.get(position).id;
                            LogUtil.e("career: " + info.career + "---" + currentJob);
                        }

                    }
                });

                dialog.show();


            }
        });


    }

    private void parseAge(final GroupBean item, ViewGroup mid) {

        // FIXME: 18/1/31 小米全屏没有底部导航栏时,在listview中用edittext调出软键盘会引起listview重绘,显示底部导航栏时没有这种现象.
        // FIXME: 18/1/31 安装不上应用的问题,insant run和 打开usb安装
        currentAge = "";

        final EditText et = ((EditText) mid.findViewById(R.id.age_et));

        et.addTextChangedListener(new MyTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {

                if (info.role.equals(item.member)) {
                    info.age = et.getText().toString();
                    currentAge = et.getText().toString();
                }
            }
        });


    }


    private View getView(View view, int id) {
        return view.findViewById(id);
    }


    private void parseMember(final ViewGroup view) {


        originalStatus(view);


        int childCount = 0;
        for (int i = 0; i < bean.memberInfos.size(); i++) {
            MemberInfo info = bean.memberInfos.get(i);
            if (info.role.equals("child")) {
                childCount++;
            }

        }
        for (int i = 0; i < bean.memberInfos.size(); i++) {
            MemberInfo info = bean.memberInfos.get(i);

            if (info.role.equals("spouse")) {
                getView(view, R.id.spouse_ly).setClickable(false);
                getView(view, R.id.spouse_check_un).setVisibility(View.VISIBLE);

            } else if (info.role.equals("father")) {
                getView(view, R.id.father_ly).setClickable(false);
                getView(view, R.id.father_check_un).setVisibility(View.VISIBLE);

            } else if (info.role.equals("mother")) {
                getView(view, R.id.mather_ly).setClickable(false);
                getView(view, R.id.mather_check_un).setVisibility(View.VISIBLE);
            }

        }

        if (childCount > 1) {
            ((TextView) getView(view, R.id.child_tv)).setText("孩子1");
        } else {
            ((TextView) getView(view, R.id.child_tv)).setText("孩子");
        }


        if (childCount == 1) {
            getView(view, R.id.child1_ly).setClickable(false);
            getView(view, R.id.child1_check_un).setVisibility(View.VISIBLE);
            getView(view, R.id.child2_ly).setVisibility(View.VISIBLE);

        } else if (childCount == 2) {

            getView(view, R.id.child1_ly).setVisibility(View.VISIBLE);
            getView(view, R.id.child1_check_un).setVisibility(View.VISIBLE);
            getView(view, R.id.child1_ly).setClickable(false);
            getView(view, R.id.child2_ly).setVisibility(View.VISIBLE);
            getView(view, R.id.child2_check_un).setVisibility(View.VISIBLE);
            getView(view, R.id.child2_ly).setClickable(false);

            getView(view, R.id.child3_layout).setVisibility(View.VISIBLE);

        } else if (childCount == 3) {
            getView(view, R.id.child1_ly).setVisibility(View.VISIBLE);
            getView(view, R.id.child1_check_un).setVisibility(View.VISIBLE);
            getView(view, R.id.child1_ly).setClickable(false);


            getView(view, R.id.child2_ly).setVisibility(View.VISIBLE);
            getView(view, R.id.child2_check_un).setVisibility(View.VISIBLE);
            getView(view, R.id.child2_ly).setClickable(false);

            getView(view, R.id.child3_layout).setVisibility(View.VISIBLE);
            getView(view, R.id.child3_check_un).setVisibility(View.VISIBLE);
            getView(view, R.id.child3_ly).setClickable(false);
        }


        int[] ids = {R.id.spouse_ly, R.id.father_ly, R.id.mather_ly, R.id.child1_ly, R.id.child2_ly, R.id.child3_ly};
        final int[] checkIds = {R.id.spouse_check, R.id.father_check, R.id.mather_check, R.id.child1_check, R.id.child2_check, R.id.child3_check};


        for (int i = 0; i < ids.length; i++) {
            final int finalI = i;
            final View ly = getView(view, ids[i]);
            if (ly.isClickable()) {

                ly.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {


                        View check = getView(view, checkIds[finalI]);

                        if (check.getVisibility() == View.VISIBLE) {
                            check.setVisibility(View.GONE);
                            checkPosition = -1;
                        } else {
                            check.setVisibility(View.VISIBLE);
                            checkPosition = finalI;
                        }

                        for (int j = 0; j < checkIds.length; j++) {
                            if (j != finalI) {
                                getView(view, checkIds[j]).setVisibility(View.GONE);
                            }

                        }

                    }
                });
            }
        }

    }

    private void originalStatus(ViewGroup view) {

        int[] checkIds = {R.id.spouse_check, R.id.spouse_check_un, R.id.father_check, R.id.father_check_un,
                R.id.mather_check, R.id.mather_check_un,
                R.id.child1_check, R.id.child1_check_un,
                R.id.child2_check, R.id.child2_check_un,
                R.id.child3_check, R.id.child3_check_un,
                R.id.child3_layout,
        };
        int[] lyIds = {R.id.child2_ly,};
        for (int id :
                lyIds) {
            getView(view, id).setVisibility(View.INVISIBLE);
        }

        for (int id :
                checkIds) {
            getView(view, id).setVisibility(View.GONE);
        }

        int[] ids = {R.id.spouse_ly, R.id.father_ly, R.id.mather_ly, R.id.child1_ly, R.id.child2_ly, R.id.child3_ly};
        for (int id :
                ids) {
            getView(view, id).setClickable(true);

        }


    }


    public void getLayouts(int checkPosition) {


        switch (checkPosition) {
            //配偶
            case 0:


                info = new MemberInfo("spouse");
                for (int i = 0; i < spouseIds.length; i++) {
                    LayoutBean layoutBean = new LayoutBean();
                    layoutBean.member = "spouse";
                    layoutBean.text = "您配偶" + spouseTexts[i];
                    layoutBean.layout = spouseIds[i];
                    if (i == spouseIds.length - 1) {
                        layoutBean.over = true;
                    }
                    layouts.add(layoutBean);
                }
                memberCall = "配偶";
                for (MemberInfo my :
                        bean.memberInfos) {
                    if (my.role.equals("my")) {
                        if (my.gender.equals("female")) {

                            info.gender = "male";
                        } else if (my.gender.equals("male")) {
                            info.gender = "female";
                        }
                        break;
                    }
                }
                break;
            //父亲
            case 1:

                info = new MemberInfo("father");
                for (int i = 0; i < fatherIds.length; i++) {
                    LayoutBean layoutBean = new LayoutBean();
                    layoutBean.member = "father";
                    layoutBean.text = "您父亲" + parentTexts[i];
                    layoutBean.layout = fatherIds[i];
                    if (i == fatherIds.length - 1) {
                        layoutBean.over = true;
                    }
                    layouts.add(layoutBean);
                }
                memberCall = "父亲";
                info.gender = "male";
                break;
            //母亲
            case 2:
                info = new MemberInfo("mother");
                for (int i = 0; i < motherIds.length; i++) {
                    LayoutBean layoutBean = new LayoutBean();
                    layoutBean.member = "mother";
                    layoutBean.text = "您母亲" + parentTexts[i];
                    layoutBean.layout = motherIds[i];
                    if (i == motherIds.length - 1) {
                        layoutBean.over = true;
                    }
                    layouts.add(layoutBean);
                }
                memberCall = "母亲";
                info.gender = "female";
                break;
            //孩子
            case 3:
                info = new MemberInfo("child");
                for (int i = 0; i < childIds.length; i++) {
                    LayoutBean layoutBean = new LayoutBean();
                    layoutBean.member = "child";
                    layoutBean.text = "您孩子" + childTexts[i];
                    layoutBean.layout = childIds[i];
                    if (i == childIds.length - 1) {
                        layoutBean.over = true;
                    }
                    layouts.add(layoutBean);
                    child_num = 1;
                }
                memberCall = "孩子";
                break;
            //孩子2
            case 4:
                info = new MemberInfo("child");
                for (int i = 0; i < childIds.length; i++) {
                    LayoutBean layoutBean = new LayoutBean();
                    layoutBean.member = "child";
                    layoutBean.text = "您第二个孩子" + childTexts[i];
                    layoutBean.layout = childIds[i];
                    if (i == childIds.length - 1) {
                        layoutBean.over = true;
                    }
                    layouts.add(layoutBean);
                    child_num = 2;
                }
                memberCall = "孩子";
                break;
            //孩子3
            case 5:
                info = new MemberInfo("child");
                for (int i = 0; i < childIds.length; i++) {
                    LayoutBean layoutBean = new LayoutBean();
                    layoutBean.member = "child";
                    layoutBean.text = "您第三个孩子" + childTexts[i];
                    layoutBean.layout = childIds[i];
                    if (i == childIds.length - 1) {
                        layoutBean.over = true;
                    }
                    layouts.add(layoutBean);
                    child_num = 3;
                }
                memberCall = "孩子";
                break;


        }


        LayoutBean home = new LayoutBean();
        home.member = "all";
        home.text = "您的家庭收支";
        home.layout = R.layout.family_item_home_income;
        layouts.add(home);

        LayoutBean finishBean = new LayoutBean();
        finishBean.member = "all";
        finishBean.layout = R.layout.family_item_finish;
        layouts.add(finishBean);

        // FIXME: 18/1/30 修复进度条的问题
        maxStep = layouts.size() - 1;
        currentStep = -1;

        seekBar.setMax(100);
        seekBar.setVisibility(View.VISIBLE);

    }

    interface OkListener {
        public void next(int position, GroupBean bean);

        public void pre(int position);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            showExitDialog("确认退出添加成员吗?");
            return false;
        } else {
            return super.onKeyDown(keyCode, event);
        }

    }


}
